﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ToolbarUI : MonoBehaviour
{
    public List<ToolbagSoltUI> list = new List<ToolbagSoltUI>();
    public ToolbagSoltUI selectedSlotUI;//保存上一次点击的物品栏
    private void Start()
    {
        InitUI();
    }
    private void Update()
    {
        for(int i = (int)KeyCode.Alpha1;i<=(int)KeyCode.Alpha9;i++)
        {
            if (Input.GetKeyDown((KeyCode)i))
            {
                if (selectedSlotUI != null)
                {
                    selectedSlotUI.UnHighlight();
                }
                int index = i-(int)KeyCode.Alpha1;//i不是等于（int）KeyCode.Alpha i 那么减去（int）KeyCode.Alpha1 的话正好数数组的大小
                selectedSlotUI = list[index];
                list[index].HIghlight();
            }
        }
        if(Input.GetKeyDown(KeyCode.Alpha0))
        {
            selectedSlotUI.UnHighlight();
        }
        //if (selectedslotui != null && selectedslotui.getdata().item.type == itemtype.hoe
        //    && input.getkeydown(keycode.space))//保证确保选择到了工具 并且选择的工具是锄头 并且按下了空格
        //{
        //    plantmanger.instance.hoeground(player.position);
        //}
    }
    public ToolbagSoltUI GetSelectSoltUI()
    {
        return selectedSlotUI;
    }
    void InitUI()
    {
        list = new List<ToolbagSoltUI>();//先创建一个数组在按照这个数组的大小创建集合
        ToolbagSoltUI[] SoltUIArray = transform.GetComponentsInChildren<ToolbagSoltUI>();//得到子物体里是SoltUI类型的
        for (int i = 0; i < SoltUIArray.Length; i++)
        {
            list.Add(SoltUIArray[i]);
        }
        UpdateUI();
    }
    public void UpdateUI()
    {
        List<SoltData> soltdataList = InventorManger.Instance.toolbarData.soltList;
        for (int i = 0; i < soltdataList.Count; i++)
        {
            list[i].SetData(soltdataList[i]);
        }
    }
}
